<template>
  <div class="Permission">
    <el-table
      :data="permissions"
      height="100%"
      line-height="20px"
      border
      style="width: 100%"
      row-key="permissionId"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="permissionId" label="权限ID"> </el-table-column>
      <el-table-column prop="url" label="权限url"> </el-table-column>
      <el-table-column prop="type" label="权限类型"> </el-table-column>
      <el-table-column prop="name" label="权限名称"> </el-table-column>
      <el-table-column prop="number" label="权限账号"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <!-- <el-button
            size="mini"
            type="danger"
            @click="handleRoles(scope.$index, scope.row)"
            >编辑角色</el-button
          > -->
          <!-- <el-button size="mini" @click="handleRoles(scope.$index, scope.row)"
            >编辑角色</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="编辑权限" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item
          v-show="false"
          label="权限ID"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.permissionId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限类型" :label-width="formLabelWidth">
          <el-input v-model="form.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限url" :label-width="formLabelWidth">
          <el-input v-model="form.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限账号" :label-width="formLabelWidth">
          <el-input v-model="form.number" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from "vuex";
// import { mapMutations } from "vuex";
import { permissionList, permissionUpdate } from "../../apis/permission";
import { Message } from "element-ui";
export default {
  name: "Permission",
  data() {
    return {
      dialogFormVisible: false,
      dialogFormRoleVisible: false,
      form: {
        name: "",
        permissionId: null,
        url: "",
        type: "",
        number: "",
      },
      formLabelWidth: "120px",
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
      this.dialogFormVisible = true;
      this.form = row;
    },
    dialogFormSubmit() {
      permissionUpdate(this.form)
        .then(() => {
          Message({
            type: "success",
            duration: 3000,
            message: "更新成功",
          });
        })
        .catch();

      this.dialogFormVisible = false;
    },
  },
  computed: {
    ...mapState(["permissions"]),
  },
  created() {
    permissionList({})
      .then((data) => {
        let _this = this;
        console.log(data);
        _this.$store.commit("setPermissions", data);
      })
      .catch(function (error) {
        console.log(error);
      });
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.Permission {
  height: 100%;
}
</style>
